package com.puppet.building182.service.impl;

import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgent;
import com.puppet.building182.pojo.Building;
import com.puppet.building182.service.BuildingService;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.List;

@Service
public class AiService {

    @Autowired
    private DashScopeAgent dashScopeAgent;
    @Autowired
    private BuildingService buildingService;

    public String call(String message){

        if("查询全部楼宇信息".equals(message)){
            StringBuffer stb = new StringBuffer();
            List<Building> allBuilding= buildingService.getAllBuildings(null);
            for(Building building:allBuilding){
                stb.append(building.getBuildingName());
            }
            return stb.toString();
        }
        ChatResponse response = dashScopeAgent.call(new Prompt(message));
        if(response==null || response.getResult()==null){
            return "AI响应断开，请稍后再试.............";
        }
        String content = response.getResult().getOutput().getText();
        return content;
    }


    public Flux<String> callString(String message){
        return dashScopeAgent.stream(new Prompt(message)).map(resp->{
            if(resp==null || resp.getResult()==null){
                return "ai响应断开,请稍后再试。。。。。。。。。。。";
            }
            String text=resp.getResult().getOutput().getText();
            return text;
        });

    }

}